package hadoop.mr05;


import org.apache.hadoop.io.WritableComparable;
import org.apache.hadoop.io.WritableComparator;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;

public class Phone extends WritableComparator implements WritableComparable<Phone> {
    private String state; // 省份
    private String county; // 市
    private long phone; // 号码

    public Phone() {
    }

    @Override
    public String toString() {
        return state+":"+county+":"+phone;
    }

    public Phone(String state, String county, long phone) {
        this.state = state;
        this.county = county;
        this.phone = phone;
    }

    public String getState() {
        return state;
    }

    public void setState(String state) {
        this.state = state;
    }

    public String getCounty() {
        return county;
    }

    public void setCounty(String county) {
        this.county = county;
    }

    public long getPhone() {
        return phone;
    }

    public void setPhone(long phone) {
        this.phone = phone;
    }

    // todo:排序规则 省份按字典序排序，如果省份一致按电话号码降序排序
    @Override
    public int compareTo(Phone o) {
        int result;
        // 1.比较省份
        int i = state.compareTo(o.state);
        if(i>0){
            // 如果省份大
            result = 1;
        }else if(i<0){
            // 如果省份小
            result = -1;
        }else {
            // 如果省份相同，按电话号码大小降序排序
            result =  phone > o.getPhone() ? -1 : (phone < o.getPhone() ? 1 :0);
        }
        return result;
    }

    @Override
    public void write(DataOutput dataOutput) throws IOException {
        dataOutput.writeUTF(state);
        dataOutput.writeUTF(county);
        dataOutput.writeLong(phone);
    }

    @Override
    public void readFields(DataInput dataInput) throws IOException {
        this.state = dataInput.readUTF();
        this.county = dataInput.readUTF();
        this.phone = dataInput.readLong();
    }

}
